The expression SELECT TOP is used to specify the number of records to select.
SQL SELECT TOP is useful for large tables with thousands of records, as returning a large number of records can affect system performance.
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number
SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number
The following is a sample from the "Customers" ("Customers") table of the "Northwind" database:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 5021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 5023 | Mexico |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
The following SQL statement selects the first three records from the "Customers" table (for SQL Server/MS Access):
SELECT TOP 3 *
FROM Customers
The following SQL statement shows an equivalent example for MySQL:
Run SQLSELECT *
FROM Customers
LIMIT 3
The following SQL statement shows an equivalent example for Oracle:
SELECT *
FROM Customers
FETCH FIRST 3 ROWS ONLY
The following SQL statement selects the first 50% of records from the "Customers" table (for SQL Server/MS Access):
SELECT TOP 50 PERCENT *
FROM Customers
The following SQL statement shows an equivalent example for Oracle:
SELECT *
FROM Customers
FETCH FIRST 50 PERCENT ROWS ONLY
The following SQL statement selects the first three records from the table "Customers" where the country is "Germany" (for SQL Server/MS Access):
SELECT TOP 3 *
FROM Customers
WHERE Country = 'Germany'
The following SQL statement shows an equivalent example for MySQL:
Run SQLSELECT *
FROM Customers
WHERE Country = 'Germany'
LIMIT 3
The following SQL statement shows an equivalent example for Oracle:
SELECT *
FROM Customers
WHERE Country = 'Germany'
FETCH FIRST 3 ROWS ONLY